﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.ManagedStreamHelpers
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.IO;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine
{
  internal static class ManagedStreamHelpers
  {
    internal static void ValidateLoadFromStream(Stream stream)
    {
      if (stream == null)
        throw new ArgumentNullException("ManagedStream object must be non-null", nameof (stream));
      if (!stream.CanRead)
        throw new ArgumentException("ManagedStream object must be readable (stream.CanRead must return true)", nameof (stream));
      if (!stream.CanSeek)
        throw new ArgumentException("ManagedStream object must be seekable (stream.CanSeek must return true)", nameof (stream));
    }

    [RequiredByNativeCode]
    internal static unsafe void ManagedStreamRead(
      byte[] buffer,
      int offset,
      int count,
      Stream stream,
      IntPtr returnValueAddress)
    {
      if (returnValueAddress == IntPtr.Zero)
        throw new ArgumentException("Return value address cannot be 0.", nameof (returnValueAddress));
      ManagedStreamHelpers.ValidateLoadFromStream(stream);
      *(int*) (void*) returnValueAddress = stream.Read(buffer, offset, count);
    }

    [RequiredByNativeCode]
    internal static unsafe void ManagedStreamSeek(
      long offset,
      uint origin,
      Stream stream,
      IntPtr returnValueAddress)
    {
      if (returnValueAddress == IntPtr.Zero)
        throw new ArgumentException("Return value address cannot be 0.", nameof (returnValueAddress));
      ManagedStreamHelpers.ValidateLoadFromStream(stream);
      *(long*) (void*) returnValueAddress = stream.Seek(offset, (SeekOrigin) origin);
    }

    [RequiredByNativeCode]
    internal static unsafe void ManagedStreamLength(Stream stream, IntPtr returnValueAddress)
    {
      if (returnValueAddress == IntPtr.Zero)
        throw new ArgumentException("Return value address cannot be 0.", nameof (returnValueAddress));
      ManagedStreamHelpers.ValidateLoadFromStream(stream);
      *(long*) (void*) returnValueAddress = stream.Length;
    }
  }
}
